from khl import Bot, Message
import os
import openai
openai.api_key = "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"
bot = Bot(token='1/MTU3OTY=/vFQr3G5Z0AbWuDbsfsEn5Q==')
def gpt(messages):
    completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages,
        temperature=0.7
    )
    res_msg = completion.choices[0].message
    return res_msg["content"].strip()


if __name__ == '__main__':
    # 维护一个列表用于存储多轮对话的信息
    messages = [{"role": "system", "content": "你现在是很有用的助手！"}]
    @bot.command(name='hello')
    async def ask(msg: Message, question: str):
        messages.append({"role": "user", "content": question})
        res_msg = gpt(messages)
        messages.append({"role": "assistant", "content": res_msg})
        await msg.reply(res_msg)

bot.run()
# everything done, go ahead now!
# now invite the bot to a server, and send '/hello' in any channel
# (remember to grant the bot with read & send permissions)